package com.yanqu.road.server.cross.service;

import com.google.protobuf.AbstractMessage;
import com.google.protobuf.InvalidProtocolBufferException;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.cross.CrossActivityRankListModel;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.pb.activity.CrossActivityRankProto;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.syncMessage.SyncMessage;
import com.yanqu.road.server.command.ICrossServiceCommand;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.activity.ActivityRankMgr;

import java.util.List;
import java.util.concurrent.CompletableFuture;


public class CrossGetActivityRankServiceCmd implements ICrossServiceCommand {
    @Override
    public CompletableFuture<AbstractMessage.Builder<?>> execute(SyncMessage.Request request) throws InvalidProtocolBufferException {
        CrossActivityRankProto.CrossActivityRankReqMsg reqMsg = CrossActivityRankProto.CrossActivityRankReqMsg.parseFrom(request.getDataBytes());
        int activityId = reqMsg.getActivityId();
        long userId = request.getSenderId();
        int bigRankType = reqMsg.getBigRankType();
        long serverId = request.getRawServerId();
        RankProto.RankListRespMsg.Builder respMsg = RankProto.RankListRespMsg.newBuilder();
        respMsg.setRankType(activityId);
        respMsg.setMyRank(-1);
        respMsg.setBigRankType(bigRankType);
        CrossActivityRankListModel rankListModel = ActivityRankMgr.getCrossActivityRankListModel(activityId);
        RankProto.RankListMsg.Builder rankListMsg = RankProto.RankListMsg.newBuilder();
        if (rankListModel != null) {
            rankListMsg = rankListModel.getRankListMsg();
            ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activityId);
            int maxRank = GameConfig.RANK_DEFAULT_MAX_COUNT;
            if (null != activityInfo) {
                int conditionType = ActivityHelper.getCrossRankConditionType(activityInfo.getType(), activityInfo.getChildType());
                maxRank = ActivityHelper.getMaxRankByConditionType(conditionType, ActivityMgr.getActivityConditionInfoList(activityInfo.getActivityId()));
            }
            while (null != rankListMsg.getRankListList() && rankListMsg.getRankListCount() > maxRank) {
                rankListMsg.removeRankList(rankListMsg.getRankListCount() - 1);
            }
            if (rankListMsg.getRankListList() != null) {
                respMsg.setMyRank(getMyRank(userId, serverId, rankListMsg.getRankListList(), maxRank));
            }
        }
        RankProto.RankListMsg.Builder tempMsg = RankProto.RankListMsg.newBuilder();
        int num = 0;
        for (RankProto.RankTempMsg msg : rankListMsg.getRankListList()) {
            tempMsg.addRankList(msg);
            num++;
            if (num >= ActivityMgr.getRankListShowNum()) {
                break;
            }
        }
        respMsg.setRankList(tempMsg);
        CompletableFuture<AbstractMessage.Builder<?>> completableFuture = new CompletableFuture<>();
        completableFuture.complete(respMsg);
        return completableFuture;
    }

    public int getMyRank(long userId, long serverId, List<RankProto.RankTempMsg> rankListList, int maxRank) {
        int rank = -1;
        int index = 0;
        synchronized (this) {
            for(RankProto.RankTempMsg userRank : rankListList){
                if(userRank.getUserId() == userId && userRank.getPlayerBaseData().getServerId() == serverId){
                    rank = index + 1;
                    break;
                }
                index++;
            }
        }
        if(rank > maxRank){
            rank = -1;
        }
        return rank;
    }
}
